package cn.demon.org.controller;


import cn.demon.basic.annotation.PreAuthorize;
import cn.demon.basic.util.AjaxResult;
import cn.demon.basic.util.PageList;
import cn.demon.org.domain.Department;
import cn.demon.org.query.DepartmentQuery;
import cn.demon.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门接口",description = "部门接口")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/department/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个对象",sn = "department:getOne")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.findById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/department + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    @PreAuthorize(name = "查询所有对象",sn = "department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/department + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(name = "分页查询",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize(name = "删除",sn = "department:delete")
    //http://localhost:8080/department/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param department
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/department + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){//添加
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    /**
     * 接口：部门树
     * @return
     */
    @ApiOperation(value = "部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}